package com.college.heyuhua.dao;

import com.college.heyuhua.model.shop.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by 赵东坡 on 2019/2/6.
 * Time:12:54
 * 宇宙无敌赵牛逼，祈求永无bug
 */
@Mapper
@Component
public interface ShopMapper {


    @Select("<script>" +
            "select * " +
            "from item " +
            "order by iid desc " +
            "limit 1000 " +
            "</script>")
    public List<Item> getItemList();



    @Select("<script>" +
            "select * " +
            "from category " +
            "order by cid desc " +
            "limit 1000 " +
            "</script>")
    public List<Category> getCategoryList();


    @Select("<script>" +
            "select * " +
            "from item " +
            "where cid = #{cid} " +
            "limit 1000 " +
            "</script>")
    List<Item> getItemListByCid(@Param("cid") Integer cid);


    @Insert("<script>" +
            "insert into cart (itemid,fck,uid) " +
            "values(#{id},#{fck},#{uid}) " +
            "</script>")
    int addcart(@Param("id") Integer id,@Param("fck") int fck,@Param("uid") long uid);


    @Update("<script>" +
            "update cart set count = #{counts} where itemid = #{id} and uid = #{uid}" +
            "</script>")
    int updatecartcount(@Param("id") Integer id,@Param("fck") int fck,@Param("uid") long uid,@Param("counts") Integer counts);

    @Select("<script>" +
            "select count(*) " +
            "from item " +
            "where iid = #{id} " +
            "</script>")
    int getCounts(@Param("id") Integer id);

    @Select("<script>" +
            "select * " +
            "from cart " +
            "where uid = #{uid} and count != 0 " +
            "</script>")
    List<Cart> getCart(@Param("uid") long uid);

    @Select("<script>" +
            "select * " +
            "from item " +
            "where iid = #{cart.itemid} " +
            "</script>")
    Item getItemFromCart(@Param("cart") Cart cart);


    @Update("<script>" +
            "update cart set count = #{counts} where itemid = #{iid} " +
            "</script>")
    int updates(@Param("iid") String iid,@Param("counts") String counts);


    @Delete("<script>" +
            "delete from cart where itemid = #{iid} and count > 0 " +
            "</script>")
    int deletes(@Param("iid") String iid);


    @Insert("<script>" +
            "insert into orderinfo (itemid,orderunique,uid) " +
            "values(#{items},#{random},#{uid}) " +
            "</script>")
    int insertOrder(@Param("items") String items,@Param("random") String random, @Param("uid") long uid);

    @Select("<script>" +
            "select * " +
            "from cart c " +
            "where c.itemid = #{itemid} " +
            "limit 1 " +
            "</script>")
    Cart getItemByItemid(@Param("itemid") String itemid);


    @Select("<script>" +
            "select count " +
            "from cart c " +
            "where c.itemid = #{iid} " +
            "limit 1 " +
            "</script>")
    int getCartInfo(@Param("iid") long iid);


    @Select("<script>" +
            "select * " +
            "from item i " +
            "where i.iid = #{iid} " +
            "limit 1 " +
            "</script>")
    Item getItemByIid(@Param("iid") String iid);


    @Insert("<script>" +
            "insert into cart (itemid,fck,uid,count) " +
            "values(#{iid},#{fck},#{uid},#{count}) " +
            "</script>")
    boolean addcartbycounts(@Param("iid") int iid, @Param("fck")int fck, @Param("uid")long uid, @Param("count")String count);



    @Select("<script>" +
            "select * " +
            "from orderinfo o " +
            "where o.uid = #{uid} " +
            "order by o.oid desc " +
            "limit 5 " +
            "</script>")
    List<Orderinfo> getOrderByuid(@Param("uid") long uid);



    @Select("<script>" +
            "select * " +
            "from take_delivery_of_goods t " +
            "where t.uid = #{uid} " +
            "limit 1 " +
            "</script>")
    Take_delivery_of_goods getTakeofgoods(@Param("uid") long uid);


    @Update("<script>" +
            "update take_delivery_of_goods set address = #{address} , tel = #{tel} where uid = #{uid} " +
            "</script>")
    boolean updateinfo(@Param("address")String address, @Param("tel")String tel,@Param("uid") long uid);


    @Delete("<script>" +
            "delete from cart where uid = #{uid} " +
            "</script>")
    boolean deletesCart(@Param("uid") long uid);


    @Insert("<script>" +
            "insert into category(cname) values(#{cname}) " +
            "</script>")
    boolean addCategory(@Param("cname") String cname);



    @Delete("<script>" +
            "delete from category where cid = #{cid} " +
            "</script>")
    boolean deletecategory(@Param("cid")String cid);



    @Insert("<script>" +
            "insert into item(itemname,price,counts,brand,PD,cid,uid) " +
            "values(#{i.itemname},#{i.price},#{i.counts},#{i.brand},#{i.PD},#{i.cid},#{i.uid}) " +
            "</script>")
    boolean addItem(@Param("i") Item item);
}
